<?php defined('BASEPATH') or exit('No direct script access allowed');

class Escort_zone_m extends MY_Model {
	function __construct(){
		parent::__construct();
		$this->load->library('visitor_contact_entity');
	}
	
	function loadYearCalendar(){
		$year = $this->input->post('year',TRUE);
		$escort_id = $this->input->post('escort_id', TRUE);
		
		$ESCORT = $this->escort_entity->init('id',$escort_id);
		$monthArray = defineJlMonth();
		
		$html = "<ul>";
		for($month=1; $month<=12; $month++){
			$WORKING_OBJ = $ESCORT->getWorkingCalendar($ESCORT->getId(), $year, $month);
			$dateArray = $WORKING_OBJ ? array_filter( explode(',',$WORKING_OBJ->dates) ) : array() ;
			$howmanyDaysInThisMonth = daysInMonth($month, $year);
			
			if(count($dateArray) == $howmanyDaysInThisMonth){
				$dateString = language_translate('services_from').' 1 '.language_translate('services_to').' '.$howmanyDaysInThisMonth;
			}else{
				$dateString = implode(', ',$dateArray);
			}	
			if($dateArray ){
				$html .= "<li><span>";
				$html .= $monthArray[$month][0].': '.$dateString;
				$html .= "</span></li>";
			}
		}
		$html .= "</ul>";
		echo $html;
	}
	
	
	function submit_visitor_send_message(){
		$visitor_name = $this->input->post('visitor_name', TRUE);
		$visitor_email = $this->input->post('visitor_email', TRUE);
		$visitor_phone = $this->input->post('visitor_phone', TRUE);
		$message_to_escort = $this->input->post('message_to_escort', TRUE);
		$captcha = $this->input->post('captcha', TRUE);
		$escort_id = $this->input->post('escort_id', TRUE);
		 
		$correctCaptcha 	= $this->session->userdata('captcha_content');
		$expTime			= $this->session->userdata('captcha_exprise_time');
		$ipAdd			= $this->session->userdata('ip_address');
		
		$captcha = strtolower($captcha);
		if( ! $this->phpvalidator->is_email($visitor_email) || strlen($visitor_email) > 45){
			echo language_translate('registration_m_email_err1');
			exit();	
		}
	 	if( $correctCaptcha != $captcha){
			echo language_translate('registration_m_captcha_err1');
			exit();
		}
	 	if( time() > $expTime ){
			echo language_translate('registration_m_captcha_err2');
			exit();
		}
		if( strlen($message_to_escort) <= 0 ){
			echo language_translate('contact_zone_empty_message');
			exit();
		}
		$newId = $this->visitor_contact_entity->createNewEntity();
		$CONTACT = $this->visitor_contact_entity->init('id', $newId);
		$CONTACT->setEscortId($escort_id);
		$CONTACT->setVisitorName($visitor_name);
		$CONTACT->setVisitorEmail($visitor_email);
		$CONTACT->setVisitorPhone($visitor_phone);
		$CONTACT->setMessage($message_to_escort);
		$CONTACT->setIpAddress($ipAdd);
		$CONTACT->update();
		
		$ESCORT = $this->escort_entity->init('id',$escort_id);
        if($ESCORT->checkEscortPermission() == 'escort'){
    		$this->email_sender->setFromEmail($visitor_email);
    		$this->email_sender->setToEmail($ESCORT->getEscortEmail());
    		$this->email_sender->setSubject(language_translate('contact_zone_subject_email_message'));
    		$this->email_sender->setEmailTitle(language_translate('contact_zone_email_title'));
    		$this->email_sender->setBody($message_to_escort);
    		$this->email_sender->sendEmail();
		}else{
		    $USER = $this->user_entity->init('id',$ESCORT->getUserId());
            $this->email_sender->setFromEmail($visitor_email);
    		$this->email_sender->setToEmail($USER->getEmail());
    		$this->email_sender->setSubject(language_translate('subject_email_message_to_agent'));
    		$this->email_sender->setEmailTitle(language_translate('contact_zone_email_title'));
    		$this->email_sender->setBody($message_to_escort);
    		$this->email_sender->sendEmail();        
		}
		
		echo 'ok';
	}
	
	
	
	////////////////////////////////////////////////////////////////////////////////////////////
	function services_load_country(){
		$current_dbprefix = $this->db->dbprefix;
		$this->db->set_dbprefix('default_');
		
		$html = "<select class='normal' name='service_country_id' id='service_country_id' >";
		$html .= "<option value='0'>".language_translate('services_page_country_option_select')."</option>";
		
		$rs = $this->db->query("SELECT * FROM ".TBL_PLACE_LIST." WHERE country_id != 0 AND state_id = 0 AND	province_id = 0 AND	city_id = 0 ")->result();
		foreach($rs as $item){
			$COUNTRY = $this->countries_entity->init('id',$item->country_id);
			//$html .= "<option value='{$item->country_id}'>".ucfirst(strtolower($COUNTRY->getCountryName())).' '.$item->price."&euro;</option>";
			$html .= "<option value='{$item->country_id}'>".ucfirst(strtolower($COUNTRY->getCountryName()))."</option>";
		}
		$html .= "</select>";
		$this->db->set_dbprefix($current_dbprefix);
		return $html;
	}
	
	function services_load_state($country_id){
		$current_dbprefix = $this->db->dbprefix;
		$this->db->set_dbprefix('default_');
		
		$html = "<select class='normal' name='service_state_id' id='service_state_id' >";
		$html .= "<option value='0'>".language_translate('services_page_state_option_select')."</option>";
		
		if($country_id != 0){
			$rs = $this->db->query("SELECT * FROM ".TBL_PLACE_LIST." WHERE country_id = $country_id AND	province_id = 0 AND	city_id = 0 ")->result();
			foreach($rs as $item){
				$state = getStateData_ioc($country_id, $item->state_id);
				if($item->state_id != 0){
					//$html .= "<option value='{$item->state_id}'>".getValueOfArray($state).' '.$item->price."&euro;</option>";
					$html .= "<option value='{$item->state_id}'>".getValueOfArray($state)."</option>";
				}
			}
		}
		$html .= "</select>";
		$html .= "<div class=\"clear\"></div>";
		$html .= loader_image_s("id='service_state_loading_image' class='hidden'");
		
		$this->db->set_dbprefix($current_dbprefix);
		return $html;
	}
	
	function services_load_province($country_id, $state_id){
		$current_dbprefix = $this->db->dbprefix;
		$this->db->set_dbprefix('default_');
		
		$html = "<select class='normal' name='service_province_id' id='service_province_id' >";
		$html .= "<option value='0'>".language_translate('services_page_province_option_select')."</option>";
		
		if($country_id != 0){
			$rs = $this->db->query("SELECT * FROM ".TBL_PLACE_LIST." WHERE country_id = $country_id AND	state_id = $state_id AND city_id = 0 ")->result();
			foreach($rs as $item){
				$province = getProvinceData_ioc($country_id,$state_id, $item->province_id);
				if($item->province_id != 0){
					//$html .= "<option value='{$item->province_id}'>".getValueOfArray($province).' '.$item->price."&euro;</option>";
					$html .= "<option value='{$item->province_id}'>".getValueOfArray($province)."</option>";
				}
			}
		}
		$html .= "</select>";
		$html .= "<div class=\"clear\"></div>";
		$html .= loader_image_s("id='service_province_loading_image' class='hidden'");
		
		$this->db->set_dbprefix($current_dbprefix);
		return $html;
	}
	
	function services_load_city($country_id, $state_id, $province_id){
		$current_dbprefix = $this->db->dbprefix;
		$this->db->set_dbprefix('default_');
		
		$html = "<select class='normal' name='service_city_id' id='service_city_id' >";
		$html .= "<option value='0'>".language_translate('services_page_city_option_select')."</option>";
		
		if($country_id != 0){
			$rs = $this->db->query("SELECT * FROM ".TBL_PLACE_LIST." WHERE country_id = $country_id AND	state_id = $state_id AND province_id = $province_id ")->result();
			foreach($rs as $item){
				$city = getCityData_ioc($country_id,$state_id,$province_id, $item->city_id);
				if($item->city_id != 0){
					//$html .= "<option value='{$item->city_id}'>".getValueOfArray($city).' '.$item->price."&euro;</option>";
					$html .= "<option value='{$item->city_id}'>".getValueOfArray($city)."</option>";
				}
			}
		}
		$html .= "</select>";
		$html .= "<div class=\"clear\"></div>";
		$html .= loader_image_s("id='service_city_loading_image' class='hidden'");
		
		$this->db->set_dbprefix($current_dbprefix);
		return $html;
	}
	
	function add_button_js(){
		$html = "<input type='button' onclick='return loadListingPlaceElement();' class='save-button' name='add_new_service' id='add_new_service' value='".language_translate('services_page_add_new_service')."' />";
		//$html .= "<input type=\"hidden\" name=\"listing_place_id\" id=\"listing_place_id\" value=\"0\" />";
		
		$html .= "<script type='text/javascript'>
					$(document).ready(function() {
						$('#service_country_id').live('change',function(){
							service_changeCountry_slb($('#service_country_id').val());
						});
						$('#service_state_id').live('change',function(){
							changeState_slb($('#service_state_id').val(),$('#service_country_id').val());
						});
						$('#service_province_id').live('change',function(){
							changeProvince_slb($('#service_state_id').val(),$('#service_province_id').val(),$('#service_country_id').val());
						});
					});

					function service_changeCountry_slb(country_id){
						$('#service_state_loading_image,#service_province_loading_image,#service_city_loading_image').show();
						$.get(BASE_URI+'home/services_reg_async/loadState',{service_country_id:country_id},function(res){
							$('#service_state_loading_image').hide();
							$('#service_state').html(res);
							
							firtElementValue = $('#service_state_id').children().first().val();
							changeState_slb(firtElementValue,country_id);
						});
					}
					
					function changeState_slb(state_id,country_id){
						$('#service_province_loading_image, #service_city_loading_image').show();
						$.get(BASE_URI+'home/services_reg_async/loadProvince',{service_state_id:state_id,service_country_id:country_id},function(res){
							$('#service_province_loading_image').hide();
							$('#service_province').html(res);
						 
							firtElementValue = $('#service_province_id').children().first().val();
							changeProvince_slb(state_id,firtElementValue,country_id);
						});
					}
					
					function changeProvince_slb(state_id,province_id,country_id){
						$('#service_city_loading_image').show();
						$.get(BASE_URI+'home/services_reg_async/loadCity',{service_state_id:state_id,service_country_id:country_id,service_province_id:province_id},function(res){
							$('#service_city_loading_image').hide();
							$('#service_city').html(res);
						});
					}
					
				</script>";
		return $html;
	}
	
	function fetch_listing_id($country_id,$state_id,$province_id,$city_id){
		$current_dbprefix = $this->db->dbprefix;
		$this->db->set_dbprefix('default_');
		
		$rs = $this->db->where(array('country_id'=>$country_id,'state_id'=>$state_id,'province_id'=>$province_id,'city_id'=>$city_id))
				->get(TBL_PLACE_LIST)->result();
		
		$this->db->set_dbprefix($current_dbprefix);
		return $rs ?$rs[0]->id:0;
	}
	
	function cancelOrder(){
		$current_dbprefix = $this->db->dbprefix;
		$this->db->set_dbprefix('default_');
		
		$id = $this->input->post('id',0);
		$str = $this->input->post('s','');
		if($str == makeSalt($id)){
			$this->db->query("DELETE FROM default_escort_invoice_lines WHERE invoice_id IN (SELECT id FROM default_escort_invoices WHERE history_id=$id)");
			$this->db->query("DELETE FROM default_escort_invoices WHERE history_id=$id");
			$rs = $this->db->where('id',$id)->get('default_escort_payment_history')->result();
			$this->db->query("DELETE FROM default_escort_contract_log WHERE id IN({$rs[0]->payment_ids})");
			$this->db->query("DELETE FROM default_escort_payment_history WHERE id=$id");
			
			$this->db->set_dbprefix($current_dbprefix);
			
			echo 'ok';
			exit();
		}
		exit();
	}
	
	
//end class
}	